/**************************************************************
This do file creates Appendix Figure 2;

**************************************************************/
capture log close
capture program drop _all
capture macro drop _all
estimates drop _all
set matsize 2000
drop _all
set more off


*UPDATE PATH NAMES;
global ched_data 
global output 

***************************************************************

use "${ched_data}analysis_sample.dta" 

xtset provcode year

*SET CONTROLS;
global controls_yr emp_f_2564_90_yr* emp_m_2564_90_yr* female_90_yr* age_90_yr* nurse_cen_pc90_yr*
global controls_yr_full emp_f_2564_90_yr* emp_m_2564_90_yr* female_90_yr* age_90_yr* nurse_cen_pc90_yr* _yrschl_2564_90_yr* urban_90_yr*  


*Create examinee rate;
gen examinee_rate=(examinees/pop_90)*100
replace examinee_rate=0 if examinees==.

*Passers out of grad pop;
gen pass_rate_grad=(passed/collegepop)*100
replace pass_rate_grad=0 if examinees==.


*******************************************************************************;
* MAIN RESULTS;
*******************************************************************************;

#delimit ;

foreach var of varlist migrateUSA_hth enroll_tot1 grad_tot1
	nurseprog pass_rate_grad grad_tot {;
	
	if "`var'"=="migrateUSA_hth" {; local t="1"; };
	if "`var'"=="enroll_tot1" {; local t="2"; };
	if "`var'"=="nurseprog" {; local t="5"; };
	if "`var'"=="grad_tot1" {; local t="14"; };
	if "`var'"=="grad_tot" {; local t="15"; };
	if "`var'"=="pass_rate_grad" {; local t="16"; };

	if "`var'"=="migrateUSA_hth" {; local w="1991"; };
	if "`var'"=="enroll_tot1" {; local w="1991"; };
	if "`var'"=="nurseprog" {; local w="1991"; };
	if "`var'"=="grad_tot1" {; local w="1998"; };
	if "`var'"=="grad_tot" {; local w="1998"; };
	if "`var'"=="pass_rate_grad" {; local w="1991"; };
	
	if "`var'"=="migrateUSA_hth" {; local c="Nurse Migrants per 100000"; };
	if "`var'"=="enroll_tot1" {; local c="Percentage Points"; };
	if "`var'"=="nurseprog" {; local c="Nursing Programs"; };
	if "`var'"=="grad_tot1" {; local c="Percentage Points"; };
	if "`var'"=="grad_tot" {; local c="Percentage Points"; };
	if "`var'"=="pass_rate_grad" {; local c=" Pass Rate out of Total Population (%)";};

	preserve;
	char year[omit]1999;
	xi: reg `var'  i.year|premigrate1990 i.year i.provcode $controls_yr if year>=1991&year<=2013 , cluster(province) noomit;
	estimates store event`t', title('var');

	foreach y of numlist 1991(1)1998 2000(1)2013 {;
			gen b_`y' = _b[_IyeaXpre_`y']*.0026469;
			gen upper_`y' = b_`y' + ( _se[_IyeaXpre_`y'] * 1.96 )*.0026469;
			gen lower_`y' = b_`y' - ( _se[_IyeaXpre_`y'] * 1.96 )*.0026469;
	};

		gen b_1999=0;
		
		keep b_* upper_* lower_* ;
		
		collapse b_* upper_* lower_*; 
		
		gen temp=1;
		reshape long b_ upper_ lower_, i(temp) j(year); 
		drop temp;
		
		drop if year<`w';
		
		twoway (line b_ year, lcolor(black) lpattern(solid))
				(line upper_  year, lcolor(black) lpattern(dot))
				(line lower_ year, lcolor(black) lpattern(dot)),
				legend(off)
				xtitle("Year") 
				yline(0, lstyle(shortdash_dot)) ylab(, nogrid) ytitle(`c')
				plotregion(fcolor(white)) graphregion(fcolor(white))
				xline(2000 2007, lpattern(solid) lcolor(gs14))
				`v';
				
		graph export "${output}Main Figures/cont_`var'.pdf", replace;

		
restore;	
};
